/*
 * DBManager.java
 *
 * Created on 4. juni 2007, 16:20
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package sja.util.db;

import sja.util.Log;

import javax.sql.RowSet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author sevi
 */
public class UnpooledDBManager implements DBManager {


    private String driverName = null;
    private String databaseConnectionURL = null;
    private String dbUsername = null;
    private String dbPassword = null;

    /**
     * Creates a new instance of DBManager
     */
    private UnpooledDBManager() {
    }

    /**
     * Constructs a DBManager with the given urls/passwords.
     *
     * @param driver   The name of the driver
     * @param url      The url to the database
     * @param username The username
     * @param password The password
     */
    public UnpooledDBManager(String driver, String url, String username, String password) {
        //super();

        driverName = driver;
        databaseConnectionURL = url;
        dbUsername = username;
        dbPassword = password;
    }

    /**
     * gets a fresh connection. When you have finished using it, you should return it to the pool by using returnConnection
     *
     * @throws SQLException if a db error occurs
     */
    public Connection getConnection() throws SQLException {

        Connection connection = null;

        //if (connection == null) {
        try {
            Class.forName(driverName);
            connection = DriverManager.getConnection(databaseConnectionURL, dbUsername, dbPassword);
        } catch (ClassNotFoundException e) {
            final String text = "Driver not found, please check driver class name";
            Log.using(UnpooledDBManager.class).error(text, e);
            throw new SQLException(text);
        }

        if (connection != null && !connection.isClosed()) {
            connection.setAutoCommit(false);
        }
        //}


        return connection;
    }

    public void returnConnection() {
    }

    public RowSet query(String sql) {
        return null;//TODO
    }

    public int execute(String sql) {
        return 0;//TODO
    }

    public Object queryForObject(String sql) {
        return null;//TODO
    }
}
